package alexAI;



public class MinMax {
	private int[][] move;
	private int end = move[0][0]; //a verifier le move de victoire
	private int maxScore,
			    minScore,
			    score;
	private final static boolean MAX = true,	//white player
								 MIN = false;	//black player
	
	public int minMax(int posActuel, boolean joueur, Board b){
		// return if winning game
		if(posActuel == end)
			return posActuel;
		//return the better move for white player
		if (MAX){
			maxScore = -1000000000;
			for (int pi = 0; pi<b.getAllValidMoves(MAX).size(); pi++){ //getAllValideMoves() c le profondeur max
				score = setValue(b.getAllValidMoves(MAX).get(pi), MAX, b);
				maxScore = max(maxScore, score);
				score = minMax(pi, MAX, b);
			}
			return maxScore;
		}
		//return the better move for black player
		else if(MIN){
			minScore = 1000000000;
			for (int pi = 0; pi<b.getAllValidMoves(MIN).size(); pi++){ //getAllValideMoves() c le profondeur max
				score = setValue(b.getAllValidMoves(MIN).get(pi), !MIN, b);
				minScore = min(minScore, score);
				score = minMax(pi, MIN, b);
			}
			return minScore;
		}
		
		//error in the system
		else{
			return (Integer) null;
		}
	}
	
	
	//return the maximum move
	private int max (int max, int score){
		if(max > score)
			return max;
		else
			return score;
	}
	
	//return the minimum move
	private int min (int min, int score){
		if(min < score)
			return min;
		else
			return score;
	}
	
	
	//envoie la value du score
	private int setValue(int[][] move,boolean MAX, Board b){
		int value;
		if (MAX){
			value = b.evalMove(move);
		}else if(!MAX){
			value = 0 - b.evalMove(move);
		}else
			value = (Integer) null;
		
		return value;
	}
}
